﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using RestSharp;
using RestSharp.Serialization.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace QntPlatform.BinanceApi.DApi
{
    public partial class DApiClient
    {

        public  Task<BaseCreateOrderOutput> CreateOrder(CreateOrderDto order)
        {
            return CreateReq(Prefix + "order", true)
            .AddObject(order)
            .PostAsync<BaseCreateOrderOutput>()
            .CheckResp(log);
        }
        public bool CreateOrderTest(CreateOrderDto order)
        {
          var req= CreateReq(Prefix + "order/test", true);
            var re = AddParamsByObj(req,order)
            .Post();
            if (re.IsSuccessful)
                return true;
            log.Warn("test清求失败:", new { re.ErrorException, re.ErrorMessage, re.StatusCode, re.ResponseStatus,re.Content });
            return false;
        }
        public IRestRequest AddParamsByObj(IRestRequest req, object obj) {
            var jsSet = new Newtonsoft.Json.JsonSerializerSettings()
            {
                Formatting = Formatting.Indented,
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
                NullValueHandling = NullValueHandling.Ignore,
                FloatParseHandling = FloatParseHandling.Decimal
            };
            jsSet.Converters.Add(new StringEnumConverter());
            var jobj=JObject.FromObject(obj, Newtonsoft.Json.JsonSerializer.Create(jsSet));
            foreach (var item in jobj)
            {
                req.AddParameter(item.Key, item.Value.ToString(),ParameterType.QueryString);
            }
            return req;
        }


    }
    public class CreateOrderDto : DApiDto
    {
        [DataMember(Order = 1)]
        public string Symbol { get; set; }

        [DataMember(Order = 2)]
        //[JsonConverter(typeof(StringEnumConverter))]
        public OrderSide Side { get; set; }

        [DataMember(Order = 3)]
        //[JsonConverter(typeof(StringEnumConverter))]
        public OrderType Type { get; set; }

        [DataMember(Order = 4)]
        //[JsonConverter(typeof(StringEnumConverter))]
        public TimeInForceEnum? TimeInForce { get; set; }


        [DataMember(Order = 5)]
        //[JsonConverter(typeof(StringDecimalConverter))]
        public decimal Quantity { get; set; }

        [DataMember(Order = 6)]
        //[JsonConverter(typeof(StringDecimalConverter))]
        public decimal? Price { get; set; }

        [DataMember(Order = 7)]
        public string NewClientOrderId { get; set; }

        [DataMember(Order = 8)]
        //[JsonConverter(typeof(StringDecimalConverter))]
        public decimal? StopPrice { get; set; }

        [DataMember(Order = 9)]
        //[JsonProperty("icebergQty")]
        //[JsonConverter(typeof(StringDecimalConverter))]
        public decimal? IcebergQuantity { get; set; }

        [DataMember(Order = 10)]
        //[JsonProperty("newOrderRespType")]
        //[JsonConverter(typeof(StringEnumConverter))]
        public NewOrderResponseType NewOrderRespType { get; set; }

        [DataMember(Order = 2)]
        //[JsonProperty("positionSide")]
        //[JsonConverter(typeof(StringEnumConverter))]
        public PositionSideType PositionSide { get; set; }

        public enum PositionSideType
        {
            BOTH,
            LONG,
            SHORT
        }
        public enum OrderSide
        {
            [EnumMember(Value = "BUY")]
            Buy,
            [EnumMember(Value = "SELL")]
            Sell,
        }
        public enum NewOrderResponseType
        {
            [EnumMember(Value = "RESULT")]
            Result,
            [EnumMember(Value = "ACK")]
            Acknowledge,
            [EnumMember(Value = "FULL")]
            Full,
        }
        public enum OrderType
        {
            [EnumMember(Value = "LIMIT")]
            Limit,
            [EnumMember(Value = "MARKET")]
            Market,
            [EnumMember(Value = "STOP_LOSS")]
            StopLoss,
            [EnumMember(Value = "STOP_LOSS_LIMIT")]
            StopLossLimit,
            [EnumMember(Value = "TAKE_PROFIT")]
            TakeProfit,
            [EnumMember(Value = "TAKE_PROFIT_LIMIT")]
            TakeProfitLimit,
            [EnumMember(Value = "LIMIT_MAKER")]
            LimitMaker,
        }
        public enum TimeInForceEnum
        {
            GTC,
            IOC,
        }
    }
    public abstract class BaseCreateOrderOutput : DApiDto
    {
        [DataMember(Order = 1)]
        public string Symbol { get; set; }

        [DataMember(Order = 2)]
        public long OrderId { get; set; }

        [DataMember(Order = 3)]
        public string ClientOrderId { get; set; }

    }

}
